package eval;

import network.Network;
import network.Rule;
import networkLoader.FattreeNetwork;
import partition.SubspacePartition;
import verifier.JiffyVerifier;
import verifier.Ports.PersistentPorts;
import verifier.util.Changes;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class FattreeEval {
    public static void main(String[] args) {
        Network network = FattreeNetwork.getNetwork(32);
//        seq(network, false);
        seq(network, true);
        SSSG(network, true);
    }

    private static void seq(Network network, boolean useFFMT) {
        JiffyVerifier verifier = new JiffyVerifier(network, new PersistentPorts());
        long s = System.nanoTime();
        if (useFFMT) {
            Changes changes = verifier.insertMiniBatch(network.getInitialRules());
            verifier.update(changes);
        } else {
            for (Rule rule : network.getInitialRules()) {
                Changes changes = verifier.insertMiniBatch(new ArrayList<>(Collections.singletonList(rule)));
                verifier.update(changes);
            }
        }
        System.out.println("$seq: " + (System.nanoTime() - s));
        System.out.println("#EC: " + verifier.predSize());
    }

    private static void SSSG(Network network, boolean useFFMT) {
        HashMap<Integer, List<Rule>> ssToRules = new HashMap<>();
        for (Rule rule : network.getInitialRules()) {
            long ssId = (rule.getMatch().longValue() >> 24);
            ssToRules.computeIfAbsent((int) (ssId), k -> new ArrayList<>());
            ssToRules.get((int) ssId).add(rule);
        }


        for (List<Rule> rules : ssToRules.values()) {
//            BDDEngine bddEngine = new BDDEngine(32);
//            HashMap<String, SubgraphPartition> nameToSubgraphPartition = FattreeNetwork.getPartitions(network, 32, bddEngine);
            SubspacePartition subspacePartition = new SubspacePartition(network, true);
            subspacePartition.run(rules, useFFMT);
            break;
        }

    }
}
